<?php

namespace app\admin\controller;

use app\admin\model\Image_box;
use think\Controller;
use think\Request;
use think\Db;
use app\admin\controller\Base;

class ImageBox extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $rel = Db::name('image_box')->paginate(6,false,[
            'type'     => 'Bootstrap',
            'var_page' => 'page',
            'path'=>'javascript:AjaxPage([PAGE]);',
            //使用函数助手传入参数
            'query' => request()->param()
        ]);
        return view('image/box', ['data'=>$rel]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        return view('image/addbox');
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $info = input('post.');

        $info['create_time'] = time();
        $result = Image_box::create($info)->getData();
        $data = [];
        if ($result['id']>0){
            $data['status'] = true;
            $data['message'] = '添加成功';
        }else{
            $data['status'] = false;
            $data['message'] = '添加失败';
        }
        return json($data);
    }

   

    
    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $info = input('put.');
        $box = new Image_box();
        $result = $box->save($info,['id'=>$id]);
        $data = [];
        if ($result>0){
            $data['status'] = true;
            $data['message'] = '添加成功';
        }else{
            $data['status'] = false;
            $data['message'] = '添加失败';
        }
        return json($data);
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $box = Image_box::get($id);
        if (!empty($box)){
            $data = [];
            if ($box->count === 0){
                $box->delete();
                $data['status'] = true;
                $data['message'] = '删除成功';
            }else{
                $data['status'] = false;
                $data['message'] = '删除失败,相册里有图片不能删除';
            }
           return json($data);
        }else{
            $this->redirect(url('admin/index/errorAdmin'));
        }
    }
}
